/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.pixels.Operator;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;

/**
 * PixelOperatorFragment
 *
 * @since 2021-05-14
 */
public class PixelOperatorAbilitySlice extends BaseAbilitySlice {
    /**
     * 抽象常量
     *
     * @param ADD 加操作
     */
    public static final int ADD = 1;
    /**
     * 抽象常量
     *
     * @param SUBSTRACT 减操作
     */
    public static final int SUBSTRACT = 2;
    /**
     * 抽象常量
     *
     * @param MULTIPLE 乘操作
     */
    public static final int MULTIPLE = 3;
    /**
     * 抽象常量
     *
     * @param DIVISION 除操作
     */
    public static final int DIVISION = 4;
    /**
     * 抽象常量
     *
     * @param BITWISE_AND 和操作
     */
    public static final int BITWISE_AND = 5;
    /**
     * 抽象常量
     *
     * @param BITWISE_OR 或操作
     */
    public static final int BITWISE_OR = 6;
    /**
     * 抽象常量
     *
     * @param BITWISE_NOT 非操作
     */
    public static final int BITWISE_NOT = 7;
    /**
     * 抽象常量
     *
     * @param BITWISE_XOR 异或操作
     */
    public static final int BITWISE_XOR = 8;
    /**
     * 抽象常量
     *
     * @param ADD_WEIGHT 像素混合
     */
    public static final int ADD_WEIGHT = 9;
    /**
     * 抽象常量
     *
     * @param KEY_TITLE Title
     */
    public static final String KEY_TITLE = "Title";
    /**
     * 抽象常量
     *
     * @param KEY_TYPE Type
     */
    public static final String KEY_TYPE = "Type";
    int type = 0;
    final float count = 2.0f;
    final float counts = 1.0f;
    final int gamm = 4;
    String titleString = null;
    private Image image1;
    private Image image2;
    private Image resultimg;

    /**
     * 获取布局id
     *
     * @return 布局id
     */
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_pixel_operator;
    }

    /**
     * 传输文本
     *
     * @param intent 意图
     */
    protected void initComponents(Intent intent) {
        if (intent.hasParameter(KEY_TITLE)) {
            titleString = intent.getStringParam(KEY_TITLE);
        } else {
            titleString = "";
        }
        type = intent.getIntParam(KEY_TYPE, 0);
        initView();
    }

    private void initView() {
        image1 = (Image) findComponentById(ResourceTable.Id_pixel_image1);
        image1.setPixelMap(ResourceTable.Media_pixel_test_1);
        image2 = (Image) findComponentById(ResourceTable.Id_pixel_image2);
        resultimg = (Image) findComponentById(ResourceTable.Id_result_image);
        image2.setPixelMap(ResourceTable.Media_pixel_test_2);
        CV4JImage cv4jImage1 = new CV4JImage(image1.getPixelMap());
        ImageProcessor imageProcessor1 = cv4jImage1.getProcessor();
        CV4JImage cv4jImage2 = new CV4JImage(image2.getPixelMap());
        ImageProcessor imageProcessor2 = cv4jImage2.getProcessor();
        ImageProcessor imageProcessor = null;
        switch (type) {
            case ADD:
                imageProcessor = Operator.add(imageProcessor1, imageProcessor2).get();
                break;
            case SUBSTRACT:
                imageProcessor = Operator.substract(imageProcessor1, imageProcessor2).get();
                break;
            case MULTIPLE:
                imageProcessor = Operator.multiple(imageProcessor1, imageProcessor2).get();
                break;
            case DIVISION:
                imageProcessor = Operator.division(imageProcessor1, imageProcessor2).get();
                break;
            case BITWISE_AND:
                imageProcessor = Operator.bitwiseAnd(imageProcessor1, imageProcessor2).get();
                break;
            case BITWISE_OR:
                imageProcessor = Operator.bitwiseOr(imageProcessor1, imageProcessor2).get();
                break;
            case BITWISE_NOT:
                imageProcessor = Operator.bitwiseNot(imageProcessor1);
                break;
            case BITWISE_XOR:
                imageProcessor = Operator.bitwiseXor(imageProcessor1, imageProcessor2).get();
                break;
            case ADD_WEIGHT:
                imageProcessor = Operator.addWeight(imageProcessor1, count, imageProcessor2, counts, gamm).get();
                break;
            default:
                break;
        }
        if (imageProcessor != null) {
            CV4JImage resultcv4jImage = new CV4JImage(imageProcessor.getWidth(),
                imageProcessor.getHeight(), imageProcessor.getPixels());
            resultimg.setPixelMap(resultcv4jImage.getProcessor().getImage().toBitmap());
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
